﻿#include "EditorMenuBarView.h"
#include "EditorMenuBarViewModel.h"

#include "Widgets/Keyboard/KeyboardWidget.h"
#include "Widgets/Positioners/HorizontalPositionerWidget.h"
#include "Widgets/Mouse/MouseWidget.h"
#include "Widgets/RectangleWidget.h"
#include "Widgets/TextWidget.h"
#include "Widgets/Window.h"

#include "Framework/Editor.h"
#include "Theme/EditorTheme.h"

#include "Window/EditorWindowGroupViewModel.h"

void REditorMenuBarView::BindViewModel(TSharedObjectPtr<REditorMenuBarViewModel> InViewModel)
{
    ViewModel = InViewModel;
}

void REditorMenuBarView::FillMenuWidgets(TSharedObjectPtr<RWidget> InMenuWidget)
{
    auto MenuRoot=NewWidget<RWidget>(InMenuWidget);
    MenuRoot->Anchor.Fill(InMenuWidget);
    RWidgetPtr MenuRootWeak=MenuRoot;

    //键盘输入组件，用于菜单快捷键和菜单是否自动弹出的依据
    //如果键盘输入组件没有焦点，菜单只有在鼠标点击时才会弹出
    //如果键盘输入组件有焦点，菜单会在鼠标掠过菜单按钮时自动弹出
    auto Keyboard=NewWidget<RKeyboardWidget>(MenuRoot);
    Keyboard->Anchor.Fill(MenuRoot);
    RKeyboardWidgetWeakPtr KeyboardWeak=Keyboard;

    auto Layout=NewWidget<RHorizontalPositionerWidget>(MenuRoot);  
    Layout->Anchor.Fill(MenuRoot);
    Layout->Spacing=2;
    RHorizontalPositionerWidgetWeakPtr LayoutWeak=Layout;

    //菜单按钮
    auto CreateMenuButton=[LayoutWeak,this,KeyboardWeak](
        const CString& InText
        ,TDelegate<TVector<SEditorContextMenuLine>> ContextMenuFunc
        ,int32_t InIndex
        )
    {
        RHorizontalPositionerWidgetPtr Layout=LayoutWeak;
        auto MenuButton=NewWidget<RMouseWidget>(Layout);
        MenuButton->Anchor.VerticalFill(Layout);
        MenuButton->Width=50;
        RMouseWidgetWeakPtr MenuButtonWeak=MenuButton;

        auto PopupMenu=[this,ContextMenuFunc,MenuButtonWeak,InIndex]()
        {
            auto MenuButtonPos=MenuButtonWeak->GetPresentAbsolutePosition();
            
            SIntPoint MenuPosition=MenuButtonWeak->GetWindow()->GetWindowPosition();
            MenuPosition.X+=MenuButtonPos.X;
            MenuPosition.Y+=MenuButtonPos.Y+MenuButtonWeak->GetPresentSize().Y;

            TDelegate<void> OnMenuClose=[this,InIndex]()
            {
                this->bIsButtonContextMenuVisible[InIndex]=false;
            };

            REditorWindowGroupViewModel::Get()->ShowContextMenu(
                 ContextMenuFunc.Execute()// ViewModel->GetFileMenu()
                ,MenuPosition
                ,350
                ,OnMenuClose
            );

            this->bIsButtonContextMenuVisible[InIndex]=true;
        };

        MenuButton->OnClicked.BindLambda([PopupMenu](
            const SMouseButtonEvent&)
        {
            PopupMenu();
            return true;
        });
        MenuButton->OnPressed.BindLambda([KeyboardWeak,MenuButtonWeak](const SMouseButtonEvent&)
        {
            KeyboardWeak->Focus();
            return true;
        });
        MenuButton->OnEntered.BindLambda([KeyboardWeak,PopupMenu]()
        {
            //自动弹出菜单
            if(!KeyboardWeak->IsFocused())
            {
                return;
            }

            PopupMenu();

        });

        auto MenuButtonBackground=NewWidget<RRectangleWidget>(MenuButton);
        MenuButtonBackground->Anchor.HorizontalFill(MenuButton);
        MenuButtonBackground->Anchor.VerticalCenterReference=MenuButton;
        MenuButtonBackground->Height=30;
        MenuButtonBackground->Radius=5;
        MenuButtonBackground->FillColor=REditor::Get()->CurrentTheme->ItemHoverColor;
        MenuButtonBackground->Visible=[MenuButtonWeak,this,InIndex]()
        {
            return MenuButtonWeak->IsHovered() || this->bIsButtonContextMenuVisible[InIndex];
        };
        auto MenuButtonText=NewWidget<RTextWidget>(MenuButton);
        MenuButtonText->Anchor.Fill(MenuButton);
        MenuButtonText->Text=InText;
        MenuButtonText->VerticalAlignment=ETextVerticalAlignment::Center;
        MenuButtonText->HorizontalAlignment=ETextHorizontalAlignment::Center;
        MenuButtonText->Color=REditor::Get()->CurrentTheme->TextColor;
        MenuButtonText->FontSize=16;
    };

    int32_t ButtonIndex=0;
    TDelegate<TVector<SEditorContextMenuLine>> GetFileMenuDelegate=[this]()
    {
        return ViewModel->GetFileMenu();
    };
    CreateMenuButton(U"文件",  GetFileMenuDelegate,ButtonIndex);
    ButtonIndex++;

    TDelegate<TVector<SEditorContextMenuLine>> GetEditMenuDelegate=[this]()
    {
        return ViewModel->GetEditMenu();
    };
    CreateMenuButton(U"编辑",  GetEditMenuDelegate,ButtonIndex);
    ButtonIndex++;

    TDelegate<TVector<SEditorContextMenuLine>> GetViewMenuDelegate=[this]()
    {
        return ViewModel->GetViewMenu();
    };
    CreateMenuButton(U"查看",  GetViewMenuDelegate,ButtonIndex);
    ButtonIndex++;

    TDelegate<TVector<SEditorContextMenuLine>> GetHelpMenuDelegate=[this]()
    {
        return ViewModel->GetHelpMenu();
    };
    CreateMenuButton(U"帮助",  GetHelpMenuDelegate,ButtonIndex);
    ButtonIndex++;

    bIsButtonContextMenuVisible.Resize(ButtonIndex);
    for(auto& IsVisible:bIsButtonContextMenuVisible)
    {
        IsVisible=false;
    }
    

}
